package com.cn.aam.checaiduo.ui.more;

import android.text.TextUtils;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.bean.AuthResultB;
import com.cn.aam.checaiduo.bean.UserB;
import com.cn.aam.checaiduo.center.CustomDialogCenter;
import com.cn.aam.checaiduo.center.UserCenter;
import com.cn.aam.checaiduo.ui.arouter.UIRouter;
import com.cn.aam.checaiduo.ui.arouter.section.RouterMore;
import com.cn.aam.checaiduo.utils.AuthUtil;
import com.cn.aam.checaiduo.utils.MyEditTextUtils;
import com.cn.aam.checaiduo.utils.MyViewsUtils;
import com.cn.aam.checaiduo.widget.WMToast;

import butterknife.BindView;
import butterknife.OnClick;


@Route(path = UIRouter.MORE.AUTH, name = "实名认证")
public class AuthActivity extends BaseActivity {


    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_identity)
    EditText etIdentity;
    @BindView(R.id.et_mobile)
    EditText etMobile;

    private String name;
    private String identity;
    private String mobile;

    @Override
    protected int layoutId() {
        return R.layout.activity_auth;
    }


    @Override
    protected void initView() {
        //toolbar
        ToolBarManager.with(this, WMContent)
                .setTitle("实名认证")
                .setBackgroundColor(R.color.colorPrimary)
                .setNavigationIcon(R.drawable.ic_nav_arrow_white);
    }

    private void getValue() {
        name = MyEditTextUtils.getContent(etName);
        identity = MyEditTextUtils.getContent(etIdentity);
        mobile = MyEditTextUtils.getContent(etMobile);
    }


    private boolean checkValue() {
        getValue();
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(identity) || TextUtils.isEmpty(mobile)) {
            WMToast.normal("认证信息必须填写完整");
            return false;
        }
        if (!TextUtils.isEmpty(AuthUtil.IDCardValidate(identity))) {
            WMToast.normal(AuthUtil.IDCardValidate(identity));
            return false;
        }
        if (!AuthUtil.isMobileNo(mobile)) {
            WMToast.normal("请输入正确的手机号码");
            return false;
        }
        return true;
    }

    @OnClick(R.id.btn_submit)
    public void onClickSubmit() {
        if (MyViewsUtils.isFastDoubleClick()) return;
        if (!checkValue())
            return;
        RetrofitHome.getInstance()
                .getInternalApi()
                .auth(AppData.getToken(), name, identity, mobile)
                .compose(RxSchedulers.<AuthResultB>toMain())
                .subscribe(new OnRequestListener<AuthResultB>(this) {
                    @Override
                    public void onNext(AuthResultB value) {
                        super.onNext(value);
                        if (value.getCode() == Const.REQUEST_FAILED) return;
                        UIRouter.getInstance().getMore().openAuthStep2Page(mContext, value.getUrl(), "");
                        changeAuthStatusByLocal();
                        finish();
                    }
                });
    }

    /**
     * 修改本地的认证信息
     */
    private void changeAuthStatusByLocal() {
        UserCenter userCenter = UserCenter.getInstance();
        UserB user = userCenter.getUser();
        user.setAuth_status("1");
        userCenter.setUser(user);
    }

    @Override
    public void onBackPressed() {
        CustomDialogCenter.getInstance()
                .showTipsDialog(mContext, "个人资料全程加密，仅用于新浪支付认证！", "退出", "下一步", new CustomDialogCenter.OnTipClick() {
                    @Override
                    public void onLeftButtonClick() {
                        finish();
                    }

                    @Override
                    public void onRightButtonClick() {

                    }
                });
    }
}
